package edu.nbut.hotel.model.dao.jdbc;

import edu.nbut.hotel.model.dao.ManagerDao;
import edu.nbut.hotel.model.dao.connect.JDBCUtil;
import edu.nbut.hotel.model.entity.pojo.Manager;
import edu.nbut.hotel.model.enums.Authority;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Repository
public class ManagerDaoImpl extends JDBCBase implements ManagerDao {
    @Override
    public List<Manager> queryAll() {
        String sql = "SELECT * FROM manager ";
        Connection con = JDBCUtil.getConnection();
        List<Manager> managerList = new ArrayList<Manager>();
        try {
            PreparedStatement ps = con.prepareStatement(sql);
            ResultSet rs = query(ps);
//        JDBCUtil.printResultSet(rs);
            while (rs.next()) {
                Manager manager = new Manager();
                manager.setId(rs.getInt(1));
                manager.setUserName(rs.getString(2));
                manager.setPassword(rs.getString(3));
                manager.setOther(rs.getString(4));
                manager.setAuthority(Authority.value(rs.getInt(5)));
                manager.setName(rs.getString(6));
                managerList.add(manager);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return managerList;
    }

    @Override
    public Manager queryById(int id) {
        String sql = "SELECT * FROM manager WHERE id = ?";
        Connection con = JDBCUtil.getConnection();
        try {
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setInt(1, id);
            ResultSet rs = query(ps);
//        JDBCUtil.printResultSet(rs);
            if (rs.next()) {
                Manager manager = new Manager();
                manager.setId(rs.getInt(1));
                manager.setUserName(rs.getString(2));
                manager.setPassword(rs.getString(3));
                manager.setOther(rs.getString(4));
                manager.setAuthority(Authority.value(rs.getInt(5)));
                manager.setName(rs.getString(6));
                return manager;
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    public Manager queryByUsername(String username) {
        String sql = "SELECT * FROM manager WHERE username = ?";
        Connection con = JDBCUtil.getConnection();
        try {
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setString(1, username);
            ResultSet rs = query(ps);
//        JDBCUtil.printResultSet(rs);
            if (rs.next()) {
                Manager manager = new Manager();
                manager.setId(rs.getInt(1));
                manager.setUserName(rs.getString(2));
                manager.setPassword(rs.getString(3));
                manager.setOther(rs.getString(4));
                manager.setAuthority(Authority.value(rs.getInt(5)));
                manager.setName(rs.getString(6));
                return manager;
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    public boolean addManager(Manager manager) throws SQLException {
        String sql = "INSERT INTO manager VALUES (?,?,?,?,?,?)";
        int id = save(sql,new Object[]{0,manager.getUserName(),manager.getPassword(),
                manager.getOther(),manager.getAuthority().value,
                manager.getName()},JDBCUtil.getConnection());
        return id != -1;
    }

    @Override
    public boolean deleteManager(int id) {
        String sql = "DELETE FROM manager WHERE id = ?";
        return saveOrUpdateOrDelete(sql,new Object[]{id});
    }
}
